import { getToken } from "@/utils/storage";
// #ifdef H5
var baseUrl = import.meta.env.VITE_APP_BASE_API;
// #endif
// #ifndef H5
var baseUrl = "http://43.154.53.144:8080/stock2c1/";
// #endif
const LOCAL_URL = ".do";
function getTokenConfig(url, header) {
  let isUSER = url.indexOf("user") > -1;
  if (header) {
    if (isUSER) {
      return {
        "content-type": "application/x-www-form-urlencoded",
        Authorization: getToken(),
      };
    } else {
      return {
        "content-type": "application/x-www-form-urlencoded",
      };
    }
  } else {
    return {};
  }
}
export function axios(method, url, data, header = true) {
  return new Promise((resolve, reject) => {
    uni.request({
      url: baseUrl + url + LOCAL_URL,
      method,
      data,
      header: getTokenConfig(url, header),
      // responseType: url.indexOf("getCode") != 1 ? "arraybuffer" : "application/json;charset=UTF-8",
      success: (res) => {
        if (res.statusCode == 200) {
          if (res.data == 1001) {
            uni.reLaunch({ url: "/pages/accessDeniy/accessDeniy" })
          } else {
            const { code  } = res.data;
            uni.hideLoading();
            if (code && code == 401) {
              uni.navigateTo({ url: "/pages/login/login" });
            }
            resolve(res.data);
          }
        } else if (res.statusCode == 401) {
          uni.navigateTo({ url: "/pages/login/login" });
          reject(res);
        } else if (res.statusCode == 400) {
          uni.showToast({
            title: "开发者某个字段或参数填写不对",
            icon: "none",
            duration: 1000,
          });
          reject(res);
        } else if (res.statusCode == 500) {
          uni.showToast({
            title: "服务器发生未知错误",
            icon: "none",
            duration: 1000,
          });
          reject(res);
        } else if (res.statusCode == 202) {
          uni.showToast({ title: res.data.msg, icon: "none", duration: 1000 });
          reject(res);
        } else {
          uni.showToast({
            title: "服务器发生未知错误",
            icon: "none",
            duration: 1000,
          });
          reject(res);
        }
      },
      complete: () => {
        uni.hideLoading();
      },
    });
  });
}
